﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security;
using System.Security.Cryptography;
namespace Common
{
   public class StringHelper
    {
       /// <summary>
       /// 获取字符串的hash值（MD5）
       /// </summary>
       /// <param name="src"></param>
       /// <param name="IsUppercase"></param>
       /// <returns></returns>
       public static string GetMd5(string src,bool IsUppercase=false)
       {
           if (string.IsNullOrEmpty(src))
           {
               return "";
           }
           StringBuilder sb = new StringBuilder(32);
           MD5 md5 = MD5.Create();
           byte [] datas= md5.ComputeHash(Encoding.UTF8.GetBytes(src));
           for (int i = 0; i < datas.Length; i++)
           {
               if (IsUppercase)
               {
                   sb.Append(datas[i].ToString("X2"));
               }
               else
               {
                   sb.Append(datas[i].ToString("x2"));
               }
           }
           return sb.ToString();
       }
       /// <summary>
       /// 获取具有掩码的字符串：如157****1235
       /// </summary>
       /// <param name="src">源字符串</param>
       /// <param name="len">替换长度</param>
       /// <param name="start">开始位置</param>
       /// <returns>掩码字符串</returns>
       public static string GetStrMask(string src,int len,int start)
       {
           if (string.IsNullOrEmpty(src))
           {
               return src;
           }
           //当掩码长度大于字符串长度时
           if (len > src.Length)
           {
               return src;
           }
          return RegexHelper.Replace(src,@"[\d|\w]","*",len,start);
       }
       
    }
}
